<?php

namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\IndexController;
use App\Models\SystemManagement\Role;
use App\Models\SystemManagement\Privilege;
use App\Models\SystemManagement\RolePrivilege;
use Illuminate\Http\Request;

/**
 * Class RoleController
 * @package App\Http\Controllers\SystemManagement
 * @group 角色管理
 */
class RoleController extends IndexController
{
    /**
     * 新增角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 角色名称
     * @bodyParam description text required 角色描述
     * @bodyParam organization_id integer required 组织ID
     * @bodyParam disable_state int 禁用状态 0反禁用-启用 1禁用
     *
     * @response 201 {"code":201,"msg":"created","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     */
    public function createRole(\App\Http\Requests\System\Role $request){
        return $this->create($request,new Role());
    }

    /**
     * 角色列表 -- 带分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id int 角色id
     * @queryParam name string 角色名称
     * @queryParam description string 描述
     * @queryParam organization_id int 组织id
     * @queryParam organization->name string 组织名称
     * @queryParam creator string 创建人
     * @queryParam disable string 禁用人
     * @queryParam disable_time datetime 禁用时间
     * @queryParam disable_state int 禁用状态 0反禁用-启用 1禁用
     *
     * @response 404 {"code":404,"msg":"not found","data":"没有查询到任何数据"}
     */
    public function getRole(){
        $per_page = request()->input('per_page');
        return $this->getList(new Role(),$per_page);
    }

    /**
     * 角色列表 -- 不带分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id int 角色id
     * @queryParam name string 角色名称
     * @queryParam description string 描述
     * @queryParam organization_id int 组织id
     * @queryParam organization->name string 组织名称
     * @queryParam creator string 创建人
     * @queryParam disable string 禁用人
     * @queryParam disable_time datetime 禁用时间
     * @queryParam disable_state int 禁用状态 0反禁用-启用 1禁用
     *
     * @response 404 {"code":404,"msg":"not found","data":"没有查询到任何数据"}
     */
    public function getAll(){
        $role = Role::getAll();
        return response()->json(encodeMsg($role,200));
    }

    /**
     * 查看角色
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required 角色id
     *
     * @queryParam id int 角色id
     * @queryParam name string 角色名称
     * @queryParam description string 描述
     * @queryParam organization_id int 组织id
     * @queryParam organization->name string 组织名称
     * @queryParam creator string 创建人
     * @queryParam disable string 禁用人
     * @queryParam disable_state int 禁用状态 0反禁用-启用 1禁用
     *
     * @response 200 {"code":200,"msg":"Success","data":{"id":1,"mark":1,"name":"\u8d85\u7ea7\u7ba1\u7406\u5458","description":"\u8d85\u7ea7\u7ba1\u7406\u5458","type":0,"attr":0,"default":0,"creator":"han","disable":null,"disable_time":null,"disable_state":0,"data_state":1,"organization_id":1}}
     * @response 404 {"code":404,"msg":"not found","data":"没有该记录"}
     */
    public function getOneRole($id){
        return $this->show($id,new Role());
    }

    /**
     * 修改角色
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required 角色id
     * @bodyParam name string required 角色名称
     * @bodyParam description text required 角色描述
     * @bodyParam organization_id integer required 组织ID
     * @bodyParam disable_state int 禁用状态 0反禁用-启用 1禁用
     *
     * @response 205 {"code":205,"msg":"updated","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     */
    public function updateRole(\App\Http\Requests\System\Role $request,$id){
        return $this->update($request,$id,new Role());
    }

    /**
     * 批量删除角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array 角色ID,批量删除+单条删除,以数组形式
     *
     * @response  204 {"code":204,"msg":"deleted","data":"删除成功"}
     * @response  400 {"code":400,"msg":"failed","data":"删除失败/数据格式错误"}
     */
    public function del(Request $request){
        return $this->delete($request,new Role());
    }

    /**
     * 批量恢复角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array 角色ID,批量恢复+单条恢复,以数组形式
     *
     * @response  204 {"code":204,"msg":""deleted,"data":"恢复成功"}
     * @response  400 {"code":400,"msg":"failed","data":"恢复失败/数据格式错误"}
     */
    public function rec(Request $request){
        return $this->recover($request,new Role());
    }

    /**
     * 搜索角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string 角色名称
     *
     * @queryParam id int 角色id
     * @queryParam name string 角色名称
     * @queryParam description string 描述
     * @queryParam organization_id int 组织id
     * @queryParam organization->name string 组织名称
     * @queryParam creator string 创建人
     * @queryParam disable string 禁用人
     * @queryParam disable_time datetime 禁用时间
     * @queryParam disable_state int 禁用状态 0反禁用-启用 1禁用
     *
     * @response 200 {"code":201,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"not found","data":"没有该记录"}
     */
    public function getBy(Request $request){
        return $this->getByCond($request,new Role());
    }

    /**
     * 获取角色的权限
     * @param Request $request
     * @return array
     */
    public function getRoleRulesTree($id){
        $roleIds = Role::getRoleRules($id);
        return response()->json(encodeMsg($roleIds,200));
    }

    /**
     * 获取角色的菜单
     * @param Request $request
     * @return array
     */
    public function getRoleMenusTree($id){
        //获取 h5 页面的菜单
        $lists = Role::getRoleRules($id, ['status' => 1, 'type' => 0]);
        $lists = Privilege::getMenusTree($lists, 0);
        return response()->json(encodeMsg($lists,200));
    }
}
